package com.tcwong.redis.listener;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * Description
 *
 * @author tcwong
 * @date 2021/11/13
 * Since 1.8
 */
@Configuration
public class RedisListenerConfiguration {

    @Bean
    public MessageListenerAdapter listenerAdapter(RedisMessageListenerService listenerService){
        MessageListenerAdapter listenerAdapter = new MessageListenerAdapter(listenerService);
        listenerAdapter.afterPropertiesSet();
        return listenerAdapter;
    }

    @Bean
    public RedisMessageListenerContainer listenerContainer(RedisConnectionFactory connectionFactory
            ,MessageListenerAdapter listenerAdapter){
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory);
        listenerContainer.addMessageListener(listenerAdapter,new ChannelTopic("listenerChannel"));
        return listenerContainer;
    }

}
